home *** CD-ROM | disk | FTP | other *** search
/ Cracking 1 / Cracking I..iso / Příklady / 3.Anti-Debugging / Přepnutí do Ring0 / SEH / SEHDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-07  |  4.9 KB  |  172 lines

  1. // SEHDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "SEH.h"
  6. #include "SEHDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CSEHDlg dialog
  16.  
  17. CSEHDlg::CSEHDlg(CWnd* pParent /*=NULL*/)
  18.     : CDialog(CSEHDlg::IDD, pParent)
  19. {
  20.     //{{AFX_DATA_INIT(CSEHDlg)
  21.         // NOTE: the ClassWizard will add member initialization here
  22.     //}}AFX_DATA_INIT
  23.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  24.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  25. }
  26.  
  27. void CSEHDlg::DoDataExchange(CDataExchange* pDX)
  28. {
  29.     CDialog::DoDataExchange(pDX);
  30.     //{{AFX_DATA_MAP(CSEHDlg)
  31.         // NOTE: the ClassWizard will add DDX and DDV calls here
  32.     //}}AFX_DATA_MAP
  33. }
  34.  
  35. BEGIN_MESSAGE_MAP(CSEHDlg, CDialog)
  36.     //{{AFX_MSG_MAP(CSEHDlg)
  37.     ON_WM_PAINT()
  38.     ON_WM_QUERYDRAGICON()
  39.     ON_BN_CLICKED(IDC_OK, OnOk)
  40.     //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CSEHDlg message handlers
  45.  
  46. BOOL CSEHDlg::OnInitDialog()
  47. {
  48.     CDialog::OnInitDialog();
  49.  
  50.     // Set the icon for this dialog.  The framework does this automatically
  51.     //  when the application's main window is not a dialog
  52.     SetIcon(m_hIcon, TRUE);            // Set big icon
  53.     SetIcon(m_hIcon, FALSE);        // Set small icon
  54.     
  55.     // TODO: Add extra initialization here
  56.     
  57.     return TRUE;  // return TRUE  unless you set the focus to a control
  58. }
  59.  
  60. // If you add a minimize button to your dialog, you will need the code below
  61. //  to draw the icon.  For MFC applications using the document/view model,
  62. //  this is automatically done for you by the framework.
  63.  
  64. void CSEHDlg::OnPaint() 
  65. {
  66.     if (IsIconic())
  67.     {
  68.         CPaintDC dc(this); // device context for painting
  69.  
  70.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  71.  
  72.         // Center icon in client rectangle
  73.         int cxIcon = GetSystemMetrics(SM_CXICON);
  74.         int cyIcon = GetSystemMetrics(SM_CYICON);
  75.         CRect rect;
  76.         GetClientRect(&rect);
  77.         int x = (rect.Width() - cxIcon + 1) / 2;
  78.         int y = (rect.Height() - cyIcon + 1) / 2;
  79.  
  80.         // Draw the icon
  81.         dc.DrawIcon(x, y, m_hIcon);
  82.     }
  83.     else
  84.     {
  85.         CDialog::OnPaint();
  86.     }
  87. }
  88.  
  89. // The system calls this to obtain the cursor to display while the user drags
  90. //  the minimized window.
  91. HCURSOR CSEHDlg::OnQueryDragIcon()
  92. {
  93.     return (HCURSOR) m_hIcon;
  94. }
  95.  
  96. void CSEHDlg::OnOk() 
  97. {
  98.     _asm
  99.     {
  100.         mov ax,ds
  101.         test al,4  // o jak² operaΦnφ systΘm se jednß?
  102.         je Not_Win9x  // skok = nejednß se o operaΦnφ systΘm Windows 9x/Me
  103.             
  104. Win9x:
  105.         push offset MyHandler  // adresa handleru
  106.         push dword ptr fs:[0]  // ulo₧enφ adresy p°edchozφho handleru
  107.         mov fs:[0],esp  // instalace
  108.             
  109.         pushfd  // ulo₧enφ EFLAGS
  110.         mov eax,esp  // ulo₧enφ hodnoty ESP, tj. adresy zßsobnφku
  111.         INT 3  // --> Ring0
  112.         // NOP zde b²t nemusφ - tento k≤d se spustφ pouze ve Windows 9x/Me
  113.         // handler tak nemusφ opravovat vzniklou chybu(p°eskoΦenφm - zm∞na 
  114.         // hodnoty EIP), proto₧e EIP ve Windows 9x/Me ukazuje na instrukci 
  115.         // nßsledujφcφ po INT 3
  116.             
  117.         /*******Ring0*******/
  118.         mov ebx,Dr7  // tato instrukce bude funkΦnφ pouze v Ring0
  119.             
  120.         // obnovenφ p∙vodnφch hodnot registr∙ p°ed skokem do Ring3
  121.         push edx  // GS
  122.         push edx  // FS
  123.         push edx  // ES
  124.         push edx  // DS
  125.         push edx  // SS    
  126.         push eax  // ESP
  127.         push dword ptr [eax]  // EFLAGS
  128.         push ecx  // CS
  129.         push offset Ring3  // EIP = adresa k≤du, odkud program pob∞₧φ op∞t v Ring3
  130.         iretd  // --> Ring3
  131.             
  132. Ring3:
  133.         popfd  // obnovenφ EFLAGS
  134.         pop dword ptr fs:[0]  // obnovenφ p∙vodnφho handleru
  135.         add esp,4  // korekce zßsobnφku
  136.         jmp Ok
  137.             
  138.         /*******MyHandler*******/
  139.         // SEH handler pro p°echod do Ring0
  140. MyHandler:
  141.         mov edx,[esp+0Ch]  // CONTEXT
  142.         mov ecx,[esp+4]  // EXCEPTION_RECORD
  143.         mov ecx,[ecx]  // ECX = Φφslo chyby, kterß prßv∞ vznikla
  144.         cmp ecx,80000003h  // jednß se o chybu STATUS_BREAKPOINT(80000003h)
  145.                            // vyvolanou instrukcφ INT 3?
  146.         jne Chyba  // skok = jednß se o jinou chybu, kterou tento handler 
  147.                    // nebude zpracovßvat
  148.         movzx ecx,word ptr [edx+0BCh]  // ECX = CS
  149.         mov [edx+0ACh],ecx  // ECX(po skoku z handleru zp∞t do programovΘho k≤du) =  CS
  150.         mov dword ptr [edx+0BCh],28h  // CS(po skoku...) = 28h, to zajiÜ¥uje
  151.                                       // p°epnutφ do Ring0
  152.         movzx ecx,word ptr [edx+0C8h]  // ECX = SS
  153.         mov [edx+0A8h],ecx  // EDX(po skoku...) = ECX = SS
  154.         mov dword ptr [edx+0C8h],30h  // SS(po skoku...) = 30h, to
  155.                                       // zajiÜ¥uje p°epnutφ do Ring0
  156.         or dword ptr [edx+0C0h],200h  // EFLAGS(po skoku...) = 200h
  157.         sub eax,eax  // ExceptionContinueExecution
  158.         ret  // --> Ring0
  159.             
  160. Chyba:
  161.         sub eax,eax
  162.         inc eax  // ExceptionContinueSearch
  163.         ret
  164.     }
  165. Not_Win9x:
  166.     MessageBox("Tato metoda funguje pouze ve Windows 9x/Me",NULL,MB_OK);
  167.     return;
  168.         
  169. Ok:
  170.     MessageBox("P°echod mezi Ring m≤dy prob∞hl ·sp∞Ün∞",NULL,MB_OK);        
  171. }
  172.